package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Override
    public void customPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectPage(page,queryVo);
    }

    @Override
    public RoomDetailVo customGetById(Long id) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (roomInfo==null){
            return  null;
        }
        RoomDetailVo roomDetailVo=new RoomDetailVo();
        //使用 Spring 提供的工具类 BeanUtils.copyProperties，将 roomInfo 对象中的属性 自动拷贝到 roomDetailVo 中。
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //设置 roomDetailVo 的 isDelete 属性，表示该房间是否已被删除。
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted()==1);

        //所属公寓信息
        ApartmentItemVo apartmentItemVo=
                apartmentInfoMapper.selectApartmentItemVo(id);
        Long apartmentId = apartmentItemVo.getId();
        BigDecimal minRent=new BigDecimal(roomInfoMapper.selectMinRent(apartmentId).toString());
        apartmentItemVo.setMinRent(minRent);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);

        //图片列表
        List<GraphVo> graphVoList=graphInfoService.selectGraphVoList(id);
        roomDetailVo.setGraphVoList(graphVoList);
        //属性信息
        List<AttrValueVo> attrValueVoList=roomInfoMapper.selectAttrValueVo(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        //配套信息
        List<FacilityInfo> facilityInfoList=roomInfoMapper.selectFacilityInfoList(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        //标签信息
        List<LabelInfo> labelInfoList=roomInfoMapper.selectLabelInfoList(id);
        roomDetailVo.setLabelInfoList(labelInfoList);
        //支付方式
        List<PaymentType> paymentTypeList=roomInfoMapper.selectPaymentTypeList(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //杂费信息
        List<FeeValueVo> feeValueVoList=roomInfoMapper.selectFeeValueVoList(apartmentId);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        //租期信息
        List<LeaseTerm> leaseTermList=roomInfoMapper.selectLeaseTermList(id);
        roomDetailVo.setLeaseTermList(leaseTermList);
        //是否入住
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id).eq(LeaseAgreement::getApartmentId,apartmentId)
                .in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        long count = leaseAgreementService.count(leaseAgreementLambdaQueryWrapper);
        roomDetailVo.setIsCheckIn(count<=0);
        return roomDetailVo;

    }

    @Override
    public void customPageByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {
        roomInfoMapper.selectPageItemByApartmentId(roomItemVoPage,id);
    }
}




